package cn.hairui.blog.controller;


import cn.hairui.blog.configure.WebLog;
import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.entity.Messagereply;
import cn.hairui.blog.entity.User;
import cn.hairui.blog.service.MessageService;
import cn.hairui.blog.service.MessagereplyService;
import cn.hairui.blog.util.DateUtil;
import com.alibaba.druid.support.json.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 留言回复表 前端控制器
 * </p>
 *
 * @author laoganbu
 * @since 2020-04-20
 */
@Controller

public class MessagereplyController {


    @Autowired
    private MessagereplyService messagereplySerivce;

    @Autowired
    private MessageService messageService;

    @PostMapping(value = "/message/reply")
    @ResponseBody
    @WebLog(description = "消息回复")
    public String messagereply(HttpServletRequest request){
        Map map = new HashMap<Object, Object>();
        User user = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if(user == null || user.getAccount() == null){
            map.put(PubConstant.flag, PubConstant.failed);
            return JSONUtils.toJSONString(map);
        }

        int replyId = Integer.parseInt(request.getParameter("replyId"));
        String replyContent = request.getParameter("replyContent");

        Messagereply messagereply = new Messagereply();
        messagereply.setMsgid(replyId);
        messagereply.setFromaccount(user.getAccount());
        messagereply.setToaccount(messageService.getById(replyId).getAccount());
        messagereply.setContent(replyContent);
        messagereply.setReplytime(DateUtil.getCurrentTime());
        messagereply.setStatus(PubConstant.APPROVE_STS_1);
        messagereplySerivce.saveOrUpdate(messagereply);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    @PostMapping(value = "/messagereply/reply")
    @ResponseBody
    @WebLog(description = "二级消息回复")
    public String messagereplysub(HttpServletRequest request){
        Map map = new HashMap<Object, Object>();
        User user = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if(user == null || user.getAccount() == null){
            map.put(PubConstant.flag, PubConstant.failed);
            return JSONUtils.toJSONString(map);
        }

        int replyId = Integer.parseInt(request.getParameter("replyId"));
        String replyContent = request.getParameter("replyContent");

        Messagereply messagereply = messagereplySerivce.getById(replyId);
        Messagereply domain = new Messagereply();

        domain.setMsgid(messagereply.getMsgid());
        domain.setFromaccount(user.getAccount());
        domain.setToaccount(messagereply.getFromaccount());
        domain.setContent(replyContent);
        domain.setReplytime(DateUtil.getCurrentTime());
        domain.setStatus(PubConstant.APPROVE_STS_1);
        messagereplySerivce.saveOrUpdate(domain);

        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

}

